using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CodeFactory.Library;
using CodeFactory.Library.Config;
using CodeFactory.Library.Language;

namespace CodeFactory.UI
{
    public partial class LanguageForm : Form
    {
        public LanguageForm()
        {
            InitializeComponent();
            try
            {
                this.InitCommonText();
                this.InitCommonData();
            }
            catch (Exception ex)
            {
                this.ShowError("LanguageForm Init Error.", ex.Message);
            }
        }

        private void InitCommonText()
        {
            this.Text = LanguageManager.StringManager["LanguageForm_Title"];
            this.group_language.Text = LanguageManager.StringManager["LanguageForm_Group"];
            this.button_save.Text = LanguageManager.StringManager["LanguageForm_Button_Save"];
        }

        private void InitCommonData()
        {
            this.list_lang.Items.Clear();
            foreach (LanguageInfo info in LanguageManager.PackList)
            {
                this.list_lang.Items.Add(info.LangName);
                if (info.LangPack == ConfigManager.Info.LanguagePack)
                {
                    this.list_lang.SelectedItem = info.LangName;
                }
            }
        }

        private void ShowError(string title, string msg)
        {
            MessageBox.Show(this, msg, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void button_save_Click(object sender, EventArgs e)
        {
            try
            {
                LanguageInfo select = null;
                foreach (LanguageInfo info in LanguageManager.PackList)
                {
                    if (info.LangName.Equals(this.list_lang.SelectedItem))
                    {
                        select = info;
                    }
                }
                if (select == null)
                {
                    this.ShowError(LanguageManager.StringManager["Error_System_Title"], LanguageManager.StringManager["LanguageForm_Msg_Null"]);
                    return;
                }
                else
                {
                    ConfigManager.Info.LanguagePack = select.LangPack;
                    ConfigManager.SaveConfig(ConfigManager.Info);
                    MessageBox.Show(this, LanguageManager.StringManager["LanguageForm_Msg_Save"], LanguageManager.StringManager["Message_System_Title"], MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Hide();
                }
            }
            catch (Exception ex)
            {
                this.ShowError("LanguageForm Init Error.", ex.Message);
            }
        }
    }
}